﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Topology;


namespace FXnaInfrastructure
{


    /// <summary>
    /// 这里分为标准网格等间距
    /// 以及不等间距偏移算法等
    /// 这里应用的是z向上，左下角为最低点，暂时
    /// 提供三维均等分空间几何
    /// </summary>
    [Serializable]
    public  class F3DGrid:FGrid
    {


        /// <summary>
        /// 提供均等分的网格函数创建过程
        /// </summary>
        /// <param name="orgX"></param>
        /// <param name="orgY"></param>
        /// <param name="orgZ"></param>
        /// <param name="nxPt"></param>
        /// <param name="nyPt"></param>
        /// <param name="nzPt"></param>
        /// <param name="xLength"></param>
        /// <param name="yLength"></param>
        /// <param name="zLength"></param>
        public F3DGrid(double rotate, double orgX,double orgY,double orgZ,int nxPt,int nyPt,int nzPt,double xLength,double yLength,double zLength):base(rotate,orgX,orgY, nxPt,nyPt,xLength,yLength)
        {


            this._orgZ = orgZ;

            this._nZ = nzPt;

            this._zLength = zLength;


            for (int i = 0; i < nxPt; i++)
            {
                this._lstXPos.Add(i*xLength/(nxPt-1));
                
            }

            for (int j = 0; j < nyPt; j++)
            {
                this._lstYPos.Add(j*yLength/(nyPt-1));
                
            }


            for (int k = 0; k < nzPt; k++)
            {

                this._lstZPos.Add(k*zLength/(nzPt-1));

                
            }





 
        }



        double _orgZ;

        public double OrgZ
        {
            get { return _orgZ; }
            set { _orgZ = value; }
        }


        int _nZ;

        public int NZ
        {
            get { return _nZ; }
            set { _nZ = value; }
        }



        double _zLength;

        public double ZLength
        {
            get { return _zLength; }
            set { _zLength = value; }
        }




        List<double> _lstXPos = new List<double>();

        List<double> _lstYPos = new List<double>();

        List<double> _lstZPos = new List<double>();




        //Coordinate[, ,] _centerCoords;


        ///// <summary>
        ///// 网格单元的中心点坐标，这里先用一下这个数据结构
        ///// 如果实在不行，在考虑更换，层次有点偏高，但也可以接受
        ///// </summary>
        //public virtual Coordinate[, ,] CenterCoords
        //{
        //    get { return _centerCoords; }
        //    set { _centerCoords = value; }
        //}




        public override double GetXPos(int i)
        {
            return this._orgX+ this._lstXPos[i];
        }

        public override double GetYPos(int j)
        {
            return this._orgY+ this._lstYPos[j];
        }


        public virtual double GetZPos(int k)
        {

            return this._orgZ+ this._lstZPos[k];

 
        }



        /// <summary>
        /// 取得某个位置的索引号
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <param name="k"></param>
        /// <returns>索引号</returns>
        public int GetIndex(int i, int j, int k)
        {
            int index;

            int rowCount = this.NY - 1;
            int colCount = this.NX - 1;

            if (j == colCount)
            {
                index = rowCount * colCount * (k - 1) + i * j;
            }
            else
            {
                index = rowCount * colCount * (k - 1) + (i - 1) * colCount + j;
            }
            return index;
        }


        /// <summary>
        /// 取得指定索引号的位置信息
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int[] GetIJK(int index)
        {
            int rowCount = this.NY - 1;
            int colCount = this.NX - 1;

            int i, j, k;
            int m = index / (rowCount * colCount);
            int n = index % (rowCount * colCount);
            if (n == 0)
            {
                k = m;
                i = rowCount;
                j = colCount;
            }
            else
            {
                k = m + 1;
                if (n % colCount == 0)
                {
                    i = n / colCount;
                    j = colCount;
                }
                else
                {
                    i = n / colCount + 1;
                    j = n % colCount;
                }
            }
            int[] location = { i, j, k };
            return location;
        }


        /// <summary>
        /// 当前网格的最大index值
        /// </summary>
        public int MaxIndex
        {
            get {

                return (this._nX - 1) * (this._nY - 1) * (this._nZ - 1);
            }
        }


    }
}
